;layui.define(function(e) {
    layui.use(['table','layer','form'], function(){
        var $ = layui.jquery
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
        //展示已知数据
       var role = table.render({
            elem: '#roleManger'
            ,cols: [[ //标题栏
                {field: 'roleName', title: '角色名称'}
                ,{field: 'tool',title: '操作', toolbar: '#roleMangerBar'}
            ]]
            ,height: 'full-220'
            ,cellMinWidth: 120
            ,data: [{
                "roleName": "培训管理"
                ,"tool": "2016-10-14"
            },{
                "roleName": "admin"
                ,"tool": "2016-10-14"
            },{
                "roleName": "运营管理员"
                ,"tool": "2016-10-14"
            },{
                "roleName": "车源管理员"
                ,"tool": "2016-10-14"
            }]
            ,page: true //是否显示分页
        });

       var user = table.render({
           elem: '#userManager'
           ,cols: [[ //标题栏
               {field: 'loginName', title: '登录账号'}
               ,{field: 'name', title: '姓名'}
               ,{field: 'phone', title: '手机号'}
               ,{field: 'email', title: '邮箱'}
               ,{field: 'class', title: '类别'}
               ,{field: 'role', title: '所属角色'}
               ,{field: 'tool',title: '操作', toolbar: '#userManagerBar',minWidth:'280'}
           ]]
           ,height: 'full-220'
           ,cellMinWidth: 120
           ,data: [{
               "loginName": "ksjfkjfalj"
               ,"name": "龚天培"
               ,"phone": "12358524256"
               ,"email": "tianpei@upincar.com"
               ,"class": "经销商"
               ,"role": "河北经销商"
               ,"tool": "操作"
           },{
               "loginName": "ksjfkjfalj"
               ,"name": "龚天培"
               ,"phone": "12358524256"
               ,"email": "tianpei@upincar.com"
               ,"class": "经销商"
               ,"role": "河北经销商"
               ,"tool": "操作"
           },{
               "loginName": "ksjfkjfalj"
               ,"name": "龚天培"
               ,"phone": "12358524256"
               ,"email": "tianpei@upincar.com"
               ,"class": "经销商"
               ,"role": "河北经销商"
               ,"tool": "操作"
           },{
               "loginName": "ksjfkjfalj"
               ,"name": "龚天培"
               ,"phone": "12358524256"
               ,"email": "tianpei@upincar.com"
               ,"class": "经销商"
               ,"role": "河北经销商"
               ,"tool": "操作"
           }]
           ,page: true //是否显示分页
       });

        //监听role表格复选框选择
        table.on('checkbox(roleListTable)', function(obj){
            console.log(obj)
        });
        //监听role表格工具条
        table.on('tool(roleListTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
               newRoles("编辑角色")
            } else if(obj.event === 'delete'){
                layer.confirm('确定删除吗?', function(index){
                    obj.del();
                    layer.close(index);
                });
            }
        });
        //监听user表格复选框选择
        table.on('checkbox(userListTable)', function(obj){
            console.log(obj)
        });
        //监听role表格工具条
        table.on('tool(userListTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'stop'){
                layer.confirm('确定停用吗?', function(index){
                    obj.del();
                    layer.close(index);
                });
            }else if(obj.event === 'delete'){
                layer.confirm('确定删除吗?', function(index){
                    obj.del();
                    layer.close(index);
                });
            }else if(obj.event === 'edit'){
                newAccount("编辑")
            } else if(obj.event === 'changePassword'){
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: "修改密码"
                    ,content: '../../set/user/password.html'
                    ,area: ['600px', '500px']
                    ,btn: ['保存', '取消']
                    ,maxmin: true
                    ,success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
//                        console.log(body.html()) //得到iframe页的body内容
                        var paramId=body.find("input[name='id']");
                        paramId.val(111);
                    }
                    ,yes: function (layero, index) {
                        layer.alert("保存按钮");
                        return false //开启该代码可禁止点击该按钮关闭
                    }
                    ,btn2: function (layero, index) {
                        layer.alert("取消按钮")
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                })
                layer.full(index);
            }
        });
        //获取role表格数据,user表格获取方式同下,根据table的id获取
        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('roleManger')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('roleManger')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('roleManger');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };
        //给方法绑定click事件
        $('.layadmin-takerates .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $("#addRole").click(function () {
            newRoles("新建角色")
        })
        $("#addAccount").click(function () {
            newAccount("新建账号")
        })
        form.render();
        function newRoles(title) {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: title
                ,content: 'addRole.html'
                ,area: ['500px', '500px']
                ,btn: ['保存', '取消']
                ,maxmin: true
                ,success: function(layero, index){
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
//                        console.log(body.html()) //得到iframe页的body内容
                    var paramId=body.find("input[name='id']");
                    paramId.val(111);
                }
                ,yes: function (layero, index) {
                    layer.alert("保存按钮");
                    return false //开启该代码可禁止点击该按钮关闭
                }
                ,btn2: function (layero, index) {
                    layer.alert("取消按钮")
                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });
            layer.full(index);
        }
        function newAccount(title) {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: title
                ,content: 'createAccount.html'
                ,area: ['500px', '500px']
                ,btn: ['保存', '取消']
                ,maxmin: true
                ,success: function(layero, index){
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
//                        console.log(body.html()) //得到iframe页的body内容
                    var paramId=body.find("input[name='id']");
                    paramId.val(111);
                }
                ,yes: function (layero, index) {
                    layer.alert("保存按钮");
                    return false //开启该代码可禁止点击该按钮关闭
                }
                ,btn2: function (layero, index) {
                    layer.alert("取消按钮")
                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });
            layer.full(index);
        }
    });
    //输出模块
    e("list", {})
});